package com.hxy.example.base;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import java.lang.ref.WeakReference;
import java.util.List;
import com.hxy.example.base.BaseViewHolder;


public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    private Context mContext;
    private int mLayoutId;
    private List<T> mData;
    private ItemClick itemClick;
    private ItemLongClick itemLongClick;
    // 普通布局
    private final int TYPE_ITEM = 1;
    // 脚布局
    private final int TYPE_FOOTER = 2;
    // 当前加载状态，默认为加载完成
    private int loadState = 2;
    // 正在加载
    public final int LOADING = 1;
    // 加载完成
    public final int LOADING_COMPLETE = 2;
    // 加载到底
    public final int LOADING_END = 3;
    //是否显示footerView
    public boolean isShowfootView = false;

    //是否拦截事件
    public  boolean isIntercept = false;

//    private  Broccoli broccoli;
    private boolean isLoading;
    public void setItemLongClick(ItemLongClick itemLongClick) {
        this.itemLongClick = itemLongClick;
    }

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public BaseRecyclerAdapter(Context mContext, int mLayoutId, List<T> mData) {
        this.mContext = (Context) new WeakReference(mContext).get();
        this.mLayoutId = mLayoutId;
        this.mData = mData;
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            BaseViewHolder viewHolder = BaseViewHolder.getRecyclerHolder(mContext, parent, mLayoutId);
            return viewHolder;
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        convert(holder,mData.get(position));
        if(isIntercept){
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return holder.itemView.onTouchEvent(event);

                }
            });
        }else{
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    if(AntiShakeUtils.isInvalidClick(view,300)){
//                        return;
//                    }
                    itemClick.setOnClick(position);
                }
            });


            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    itemLongClick.setOnLongClick(position);
                    return false;
                }
            });
        }




    }


    @Override
    public int getItemCount() {
        if(isShowfootView){
            return mData == null ? 0 : mData.size() + 1;
        }else{
            return  mData == null ? 0 : mData.size();
        }

    }


    /**
     * 对外提供的方法
     */
    public abstract void convert(BaseViewHolder holder, T t);

    /**
     * 普通点击事件
     */
    public interface ItemClick{
        void setOnClick(int index);
    }

    /**
     * 长按点击事件
     */
    public interface ItemLongClick{
        void setOnLongClick(int index);
    }

    /**
     * 针对多种类型的itemView
     * @param <T>
     */
    public interface ConmonItemType<T> {
        int getLayoutId(int itemType); //不同的Item的布局

        int getItemViewType(int position, T t); //type
    }



}
